package de.fhkl.android.activities;

import java.util.ArrayList;
import java.util.List;

import de.fhkl.android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class CafeteriaActivity extends Activity {
	private String tag = "MensaActivity";
	private List<String> group;
	private List<List<String>> child;

	public void onCreate(Bundle savedInstanceState) {
		Log.i(tag, "begin");
		super.onCreate(savedInstanceState);
		View view = createView();
		setContentView(view);
		
	}

	private View createView() {
		ExpandableListView view = new ExpandableListView(this);
		initializeData();
		view.setAdapter(new MenuListAdapter(this));
		view.setBackgroundResource(R.drawable.ic_launcher);
		return view;
	}

	private void initializeData() {
		group = new ArrayList<String>();
		child = new ArrayList<List<String>>();

		addInfo("Andy", new String[] { "male", "138123***", "GuangZhou" });
		addInfo("Fairy", new String[] { "female", "138123***", "GuangZhou" });
		addInfo("Jerry", new String[] { "male", "138123***", "ShenZhen" });
		addInfo("Tom", new String[] { "female", "138123***", "ShangHai" });
		addInfo("Bill", new String[] { "male", "138231***", "ZhanJiang" });

	}

	private void addInfo(String g, String[] c) {
		group.add(g);
		List<String> childitem = new ArrayList<String>();
		for (int i = 0; i < c.length; i++) {
			childitem.add(c[i]);
		}
		child.add(childitem);
	}

	class MenuListAdapter extends BaseExpandableListAdapter {
		private Context context;

		public MenuListAdapter(Context context) {
			this.context = context;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return child.get(groupPosition).get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			String string = child.get(groupPosition).get(childPosition);
			return getGenericView(string);
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return child.get(groupPosition).size();
		}

		// -------------Group-----------//
		public Object getGroup(int groupPosition) {
			return group.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			return group.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			String string = group.get(groupPosition);
			return getGenericView(string);
		}

		// --------------------Create view----------------//
		public TextView getGenericView(String s) {
			// Layout parameters for the ExpandableListView
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT, 40);
			TextView text = new TextView(context);
			text.setLayoutParams(lp);
			// Center the text vertically
			text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			// Set the text starting position
			text.setPadding(36, 0, 0, 0);

			text.setText(s);
			return text;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return false;
		}
	}
}
